package com.sys.common.config;

import com.google.common.util.concurrent.ThreadFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author LGY
 * Create by 2025/5/16 10:49
 */
@Configuration
public class ThreadPoolExecutorConfig {

    @Bean("myExecutor")
    public ThreadPoolExecutor myExecutor() {
        return new ThreadPoolExecutor(
                10, // 核心线程数
                20, // 最大线程数
                60, // 空闲线程存活时间（秒）
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(100), // 阻塞队列
                new ThreadFactoryBuilder().setNameFormat("my-executor-%d").build(), // 线程工厂
                new ThreadPoolExecutor.CallerRunsPolicy() // 拒绝策略
        );
    }
}
